using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Modules.NBrightEspace.Components;
using DotNetNuke.Services.Exceptions;
using NBrightCore.common;
using NBrightCore.render;
using NBrightDNN;

namespace DotNetNuke.Modules.NBrightEspace
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The EditNBrightEspace class is used to manage content
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class Settings : EspaceModuleBase
    {

        #region Event Handlers

        private const string EntityTypeCode = "SETTINGS"; // change for DB records


        override protected void OnInit(EventArgs e)
        {
            //Set base EntityTypeCode, MUST be done for all NBright child ctrls before OnInit
            base.EntityTypeCode = EntityTypeCode;
            
            base.OnInit(e);

            rpData.ItemTemplate = new GenXmlTemplate(TemplCtrl.GetTemplateData(EntityTypeCode + "_Detail.html", Utils.GetCurrentCulture()));

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (Page.IsPostBack == false)
            {
                PageLoad();
            }
        }

        private void PageLoad()
        {
            try
            {
                // for "SETTINGS" only 1 entry exists in table, so find that itemid
                var obj = ObjEspaceCtrl.GetByType(-1, -1, "SETTINGS");
                if (obj != null)
                {
                    base.ItemId = obj.ItemID.ToString("");
                }
                else
                {
                    //New setting page
                    base.ItemId = "0";
                }
                base.DoDetail(rpData);

                if (GenXmlFunctions.GetField(rpData, "txtConfig") == "")
                {
                    // no previous setp, so use default config data
                    var configData = TemplCtrl.GetTemplateData("config_Default.txt", Utils.GetCurrentCulture(), false, false);
                    GenXmlFunctions.SetField(rpData, "txtConfig", configData);
                }
                else
                {
                    // it's not a new install, so we don;t want default, but take the config from the filesystem.
                    var configData = TemplCtrl.GetTemplateData("config.txt", Utils.GetCurrentCulture(),false,false);
                    GenXmlFunctions.SetField(rpData, "txtConfig", configData);
                }                

            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }



        #endregion

        #region  "Events "

        protected void CtrlItemCommand(object source, RepeaterCommandEventArgs e)
        {
            var cArg = e.CommandArgument.ToString();

            switch (e.CommandName.ToLower())
            {
                case "update":
                    UpdateData();
                    var configData = GenXmlFunctions.GetField(rpData, "txtConfig");
                    TemplCtrl.SaveTemplate("config.txt",configData); // save to default
                    TemplCtrl.SaveTemplate("config.txt", Utils.GetCurrentCulture(), configData); // save to this langauge

                    Response.Redirect(EditUrl(EntityTypeCode));
                    break;
            }

        }


        #endregion

        #region "presistance methods"


        private void UpdateData()
        {
                if (Page.IsValid)
                {
                    // Does standard update of template form
                    var objInfo = (NBrightInfo)base.UpdateDetailNoValidate(rpData, EntityTypeCode,"");

                    // Special Processing
                    var objCtrl = new EspaceController();
                    GenXmlFunctions.SetHiddenField(rpData, "HomeDirectoryMapPath", PortalSettings.HomeDirectoryMapPath);
                    GenXmlFunctions.SetHiddenField(rpData, "controlMapPath", MapPath(ControlPath));
                    GenXmlFunctions.SetHiddenField(rpData, "moduleid", ModuleId.ToString(""));
                    objInfo.XMLData = GenXmlFunctions.GetGenXml(rpData);
                    objCtrl.Update(objInfo);

                }
        }



        #endregion


    }

}